import java.awt.*;
import java.awt.event.*;
import java.io.File;

import javax.swing.BoxLayout;
import javax.swing.JFileChooser;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Sdsl2BdGui implements ActionListener
{
	private TextField filePath;
	
	private Button browseButton;
	private Button loadSdslButton;
	private Button patternButton;
	
	private Label sdslPath;
	public TextArea reportText;
	 
	private final JFileChooser fc = new JFileChooser();
	FileNameExtensionFilter filter = new FileNameExtensionFilter("only txt files", "txt", "TXT");
	    
	SDSLLoader loader = new SDSLLoader();
	CsvSenzorExporter csv = new CsvSenzorExporter();

	public Sdsl2BdGui()
	{
		Frame mainFrame = new Frame("SDSL TO DB");
		mainFrame.setSize(800, 600);
		mainFrame.setResizable(true);
		mainFrame.setVisible(true);
		mainFrame.setBackground(Color.GRAY);
		mainFrame.addWindowListener(new WindowAdapter()
		{
			public void windowClosing(WindowEvent e)
			{
				System.exit(0);
			}
	    });
	    
		Panel fileForm    = new Panel();
		Panel buttonsForm = new Panel();
		Panel reportForm  = new Panel();
		Panel mainPanel   = new Panel();
		
		sdslPath = new Label("Path:");
		filePath = new TextField(40);
		
		browseButton = new Button("Browse");
		browseButton.addActionListener(this);
	  
		reportText = new TextArea();
		reportText.setEditable(false);
		
		loadSdslButton = new Button("Load SDSL");
		loadSdslButton.addActionListener(this);
		
		patternButton = new Button("Check for patterns");
		patternButton.addActionListener(this);
		
		fileForm.add(loadSdslButton);
		fileForm.add(sdslPath);
		fileForm.add(filePath);
		fileForm.add(browseButton);
		
		buttonsForm.add(patternButton);
		
		reportForm.setLayout(new BoxLayout(reportForm,BoxLayout.Y_AXIS));
		
		mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.Y_AXIS));
		mainPanel.add(fileForm,BorderLayout.NORTH);
		mainPanel.add(buttonsForm,BorderLayout.NORTH);
		
		mainFrame.add(mainPanel,BorderLayout.NORTH);
		mainFrame.add(reportText,BorderLayout.CENTER);
		
		fc.setFileFilter(filter);
	}
	
	@Override
	public void actionPerformed(ActionEvent e)
	{
		// TODO Auto-generated method stub
		if(e.getActionCommand().equals("Load SDSL"))
		{
			loader.readFile(filePath.getText(), this);
		}
		
		if(e.getActionCommand().equals("Check for patterns"))
		{
			csv.extractCSV(this, csv.dbConector.conn, "Subiect", "sdsl", 0);
		}
		
		if(e.getActionCommand().equals("Browse"))
		{
			int returnVal = fc.showOpenDialog(this.browseButton);
			if (returnVal == JFileChooser.APPROVE_OPTION) 
			{
				File file = fc.getSelectedFile();
				filePath.setText(file.getAbsolutePath());
			}
		}
	}
}